// pages/message/message.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req:false,
    totalNumber: 0,
    totalNumberapply:0,
    show: false,
    active: 0,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    phone:false,
    phoneshow:false,
    status:null,
  },
  getTotalUnRead: function() {
    var that = this;
    app.req({
      url: "orgmini/notice/newReadCount",
      success: function(res) {
        if (200 == res.code) {
          that.toggle("totalNumber", res.data.sysMessageCount);
          that.toggle("totalNumberapply", res.data.applyMessageCount);
          if (res.data.sysMessageCount > 0 || res.data.applyMessageCount > 0) {
            wx.showTabBarRedDot({
              index: 2
            })
          } else {
            wx.hideTabBarRedDot({
              index: 2,
            })
          }
        }
      }
    });
  },
  onChange(e) {
    // console.log(e)
    if (e.detail.index != this.data.active) {
      this.toggle("status", null);
      this.toggle("req", false);
      this.toggle('active', e.currentTarget.dataset.index)
      this.toggle("param.pageNumber", 1);
      this.toggle("pageEnd", false);
      this.toggle("list", []);
      this.list()
    }
  },
  list: function(append) {
    var that = this;
    var tabActive = that.data.active;
    if (0 == tabActive) {
      var url = "orgmini/message/orgMessage";
    } else if (1 == tabActive) {
      var url = "orgmini/message/orgSysMessage";
    }
    var param = that.data.param
    app.req({
      url: url,
      data: param,
      success: function(res) {
        if (200 == res.code) {
          var list = res.data.rows;
          if (0 == tabActive) {
            // for (var item of list) {
            //   if (item.type == 1) {
            //     item.typeStr = '资讯'
            //   } else if (item.type == 2) {
            //     item.typeStr = '资源'
            //   } else if (item.type == 3) {
            //     item.typeStr = '通知'
            //   } else if (item.type == 4) {
            //     item.typeStr = '党建'
            //   } else if (item.type == 5) {
            //     item.typeStr = '新人'
            //   } else if (item.type == 6) {
            //     item.typeStr = '好友'
            //   } else if (item.type == 0) {
            //     item.typeStr = '活动'
            //   }
            // }
          }
          if (!list || list.length < param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
        }
      }
    });
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  allRead(){
    var that = this;
    var type=''
    if(that.data.active==0){
      type=2
    } else if (that.data.active == 1){
      type = 3
    }
    app.req({
      url: "orgmini/notice/newReadAllMessage",
      data: {
        type:type
      },
      success: function (res) {
          that.refreshList();
      }
    });
  },
  allDel() {
    var that = this;
    var type = ''
    if (that.data.active == 0) {
      type = 2
    } else if (that.data.active == 1) {
      type = 3
    }
    wx.showModal({
      content: '是否确认删除',
      cancelText:'再想想',
      cancelColor: '#999999',
      confirmColor: '#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: "orgmini/notice/newDelAllMessage",
            data: {
              type: type
            },
            success: function (res) {
              that.refreshList();
            }
          });
        }
      }
    })
  },
  setRead: function(id, type) {
    var that = this;
    app.req({
      url: "orgmini/notice/readMessage",
      data: {
        id: id,
        type: type
      },
      success: function(res) {
        console.log(res);
       
      }
    });
  },
  systemTurn: function(e) {
    var that = this;
    this.toggle("show", true);
    var dataset = e.currentTarget.dataset;
    var list = that.data.list;
    list[dataset.index].isRead = 1;
    this.toggle("list", list);
    var item = dataset.item;
    var type = item.type * 1;
    this.setRead(item.id, item.type);
    switch (type) {
      case 63:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 62:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 61:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 60:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 43:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 42:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 51:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 47:
        //组织有新申请
        //组织审核页
        var url = "/pages/orgHome/orgHome?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 46:
        //组织有新申请
        //组织审核页
        var url = "/pages/orgHome/orgHome?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 45:
        //组织有新申请
        //组织审核页
        var url = "/pages/orgHome/orgHome?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 37:
        //组织有新申请
        //组织审核页
        var url = "/pages/orgHome/orgHome?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 36:
        //组织有新申请
        //组织审核页
        var url = "/pages/orgHome/orgHome?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 90:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + '&origin=home'
        that.navigateTo(url);
        break;
      case 89:
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId +'&origin=home'
        that.navigateTo(url);
        break;
      case 35:
        //组织有新申请
        //组织审核页
        var url = "/pages/check/check?id=" + item.orgId;
        that.navigateTo(url);
        break;
      case 52:
        //提现
        //我的钱包页
        var url = "/packageA/pages/myMoneyRecord/myMoneyRecord";
        that.navigateTo(url);
        break;
      case 53:
        //留言消息
        //留言页面id=ff29abb9bff811e9864b00163e05ff66&orgId=d8440777bf0211e9864b00163e05ff66
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 54:
        //留言消息
        //留言页面
        var url = "/pages/contentDetails/contentDetails?id=" + item.orgId + "&orgId=1";
        that.navigateTo(url);
        break;
      case 50:
        //好友点评
        //个人资料也
        var url = "/pages/myCardNew/myCardNew";
        that.navigateTo(url);
        break;
      case 2:
        //好友申请
        // 好友审核页
        var url = "/pages/friendApply/friendApply?type=1&messageId=" + item.id + "&friendId=" + item.friendId + "&status=" + item.status;
        that.navigateTo(url);
        break;
      case 4:
        //解除好友
        //接触人个人资料页
        var url = "/pages/friendApply/friendApply?type=4&messageId=" + item.id + "&friendId=" + item.friendId;
        that.navigateTo(url);
        break;
      case 5:
        //同意好友申请
        //好友名片页
        var url='/pages/myCardNew/myCardNew?type=visitor&id=' + item.friendId + '&name=' + '好友';
        that.navigateTo(url);
        break;
      case 6:
        //拒绝好友申请
        //
        var url = "/pages/friendApply/friendApply?type=3&messageId=" + item.id + "&friendId=" + item.friendId;
        that.navigateTo(url);
        break;
      case 88:
        //生日提醒
        var url = "/pages/birthday/birthday?id=" + item.orgId + "&limit=true";
        that.navigateTo(url);
        break;
    }
  },
  navigateTo: function(url) {
    wx.navigateTo({
      url: url
    })
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var item = dataset.item
    this.setRead(item.id, item.type);
    if (item.type == 35) {
      var url = '/pages/check/check?id=' + item.orgId + '&sendType=' + item.sendType;
      wx.navigateTo({
        url: url
      })
    } else if (item.type == 2){
      var url = "/pages/friendApply/friendApply?type=1&messageId=" + item.id + "&friendId=" + item.friendId + "&status=" + item.status;
      wx.navigateTo({
        url: url
      })
    }
    // if (0 == type) {
    //   var that = this;
    //   wx.navigateToMiniProgram({
    //     appId: app.globalData.activityMiniAppId,
    //     envVersion: app.globalData.activityMiniEnvVersion,
    //     path: 'pages/register/register?id=' + item.id,
    //     success(res) {
    //       console.log(res);
    //     }
    //   })
    // }

  },
  touchS(e) {
    // 获得起始坐标
    this.startX = e.touches[0].clientX;
    this.startY = e.touches[0].clientY;
  },
  touchM(e) {
    var index = e.currentTarget.dataset.index
    // 获得当前坐标
    this.currentX = e.touches[0].clientX;
    this.currentY = e.touches[0].clientY;
    const x = this.startX - this.currentX; //横向移动距离
    const y = Math.abs(this.startY - this.currentY); //纵向移动距离，若向左移动有点倾斜也可以接受
    if (x > 35 && y < 110) {
      //向左滑是显示删除
      this.setData({
        status: index
      })
    } else if (x < -35 && y < 110) {
      //向右滑
      this.setData({
        status: null
      })
    }
  },
  del(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    console.log('删除id', id)
    wx.showModal({
      content: '是否要将此项删除',
      cancelColor: '#999999',
      confirmColor: '#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: "orgmini/sys/message/deleteOne",
            data: {
              id: id
            },
            success: function (res) {
              that.toggle("status", null);
              that.toggle("req", false);
              that.toggle("param.pageNumber", 1);
              that.toggle("pageEnd", false);
              that.toggle("list", []);
              that.list()
              that.getTotalUnRead()
            }
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    if (app.searchWord){
      if (app.searchWord == 'val'){
        this.setData({
          active: 1
        })
      } else if(app.searchWord == 'val1') {
        this.setData({
          active: 0
        })
      }
      app.searchWord = ''
    }
    if (this.data.show) {
      this.toggle("show", false);
      this.getTotalUnRead();
      return;
    }
    this.refreshList();
    wx.setNavigationBarTitle({
      title: '消息中心',
    })
  },
  toggleToast1(e) {
    console.log(e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  refreshList: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if (wx.getStorageSync('uid') == '') {
        } else {
          // var phone = wx.wx.getStorageSync("phone")
          if (wx.getStorageSync("phone") ==''){
            that.toggle("phoneshow", true);
          }else{
            that.toggle("phoneshow", false);
          app.getUserInfo(function (userInfo) {
            that.toggle("userInfo", userInfo);
          }, true)
          }
        }
      },
    })
      that.toggle("status", null);
      that.toggle("req", false);
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.toggle("list", []);
      that.getTotalUnRead();
      that.list();
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.toggle("status", null);
    this.refreshList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})